import { useReducer } from "react"

const reducer=(state,action)=>{  //纯函数定义 1.不能执行带副作用的调用(包括 ajax,打开数据库,console.log)
   switch(action.type){      
       case 'incr': 
          return{count:state.count+1} 
          case 'decr': 
          return{count:state.count-1}           // 2.返回值留不能有类似Math.random()/Date.now()
       default:                                //3.不能改变参数或环境里的变量值
           return state
   }
}



const ReduceDemo=()=>{
    const [state,dispatch]=useReducer(reducer,{count:0})
    return(
        <div>
            <h2>列子</h2>
            计数器{state.count}
            <button onClick={()=>dispatch({type:'incr'})}>+</button>
            <button onClick={()=>dispatch({type:'decr'})}>-</button>
            <button onClick={()=>dispatch({type:'iner'})}>2
            +</button>


        </div>
    )
}
export default ReduceDemo